/**
 * @file Backend.hpp
 * @author rui.liang (rui.liang@ruhnix.com)
 * @brief 
 * @version 1.0
 * @date 2025-10-10
 * 
 * @copyright Copyright (c) 2025
 * 
 */

#ifndef BACKEND_H
#define BACKEND_H

#include <QDateTime>
#include <QImage>
#include <QObject>
#include <QString>
#include <QTimer>
#include <QVideoFrame>
#include <QVideoSink>

class Backend : public QObject {
    Q_OBJECT

    Q_PROPERTY(QString velocity READ velocity NOTIFY statusUpdate)

    Q_PROPERTY(QString dateTime READ dateTime NOTIFY timerUpdate)

  public:
    explicit Backend(QObject *parent = nullptr);
    ~Backend() override;

    // 速度相关
    QString velocity() const { return QString::number(m_velocity); }

    // 时间相关(yyyy-MM-dd HH:mm:ss)
    QString dateTime() const {
        return QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
    }

  public slots:

    void setCamera1VideoSink(QVideoSink *sink) { m_sinkCamera1 = sink; }

    void setCamera2VideoSink(QVideoSink *sink) { m_sinkCamera2 = sink; }

    void pushCamera1Frame(const QImage &img) {
        QVideoFrame frame(img);
        if (m_sinkCamera1) {
            m_sinkCamera1->setVideoFrame(frame);
        }
    }

    void pushCamera2Frame(const QImage &img) {
        QVideoFrame frame(img);
        if (m_sinkCamera2) {
            m_sinkCamera2->setVideoFrame(frame);
        }
    }

  signals:
    void statusUpdate();
    void timerUpdate();

  private:
    uint32_t m_velocity = 0;
    QTimer m_timer;
    QVideoSink *m_sinkCamera1 = nullptr;
    QVideoSink *m_sinkCamera2 = nullptr;
};

#endif // BACKEND_H